home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SLIDER.PAK / SLIDERX.CPP < prev   
C/C++ Source or Header  |  1997-05-06  |  6KB  |  250 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/slider.h>
  9. #include <owl/gauge.h>
  10. #include <owl/static.h>
  11. #include <owl/gdiobjec.h>
  12. #include <stdio.h>
  13.  
  14. const uint16 IDC_THERMOSTAT = 201;
  15. const uint16 IDC_HEATERTIME = 202;
  16. const uint16 IDC_OUTSIDETEMP = 203;
  17. const uint16 IDC_STATICTEMP = 205;
  18. const uint16 IDC_STATICTIME = 206;
  19. const uint16 IDC_STATICOTEMP = 207;
  20. const uint16 IDC_THERMOMETER = 208;
  21. const uint IDT_REFRESH = 1;
  22. const int  Hysteresis = 0;
  23.  
  24. //
  25. // class TTestWindow
  26. //
  27. class TTestWindow : public TWindow {
  28.   public:
  29.     TTestWindow();
  30.    ~TTestWindow();
  31.  
  32.   protected:
  33.     // overridden
  34.     //
  35.     void    SetupWindow();
  36.  
  37.     void    UpdateTemp();
  38.     void    UpdateHeaterTime(uint = 0);
  39.     void    UpdateOTemp(uint = 0);
  40.  
  41.     // handle messages
  42.     //
  43.     bool    EvEraseBkgnd(HDC);
  44.     HBRUSH  EvCtlColor(HDC, HWND hWndChild, uint ctlType);
  45.     void    EvSysColorChange();
  46.     void    EvTimer(uint timerId);
  47.  
  48.     TSlider*  Thermostat;
  49.     TSlider*  HeaterTime;
  50.     TSlider*  OutsideTemp;
  51.     TStatic*  StaticTemp;
  52.     TStatic*  StaticTime;
  53.     TStatic*  StaticOTemp;
  54.     TGauge*   Thermometer;
  55.     TBrush*   BkBrush;
  56.  
  57.     int       Temp;
  58.     int       HeaterTimeLeft;
  59.  
  60.  
  61.   DECLARE_RESPONSE_TABLE(TTestWindow);
  62. };
  63.  
  64. DEFINE_RESPONSE_TABLE1(TTestWindow, TWindow)
  65.   EV_WM_ERASEBKGND,
  66.   EV_WM_CTLCOLOR,
  67.   EV_WM_SYSCOLORCHANGE,
  68.   EV_WM_TIMER,
  69.   EV_CHILD_NOTIFY_ALL_CODES(IDC_HEATERTIME, UpdateHeaterTime),
  70.   EV_CHILD_NOTIFY_ALL_CODES(IDC_OUTSIDETEMP, UpdateOTemp),
  71. END_RESPONSE_TABLE;
  72.  
  73. TTestWindow::TTestWindow()
  74. :
  75.   TWindow(0, 0, 0)
  76. {
  77.   Attr.X = 20;
  78.   Attr.Y = 20;
  79.   Attr.W = 380;
  80.   Attr.H = 200;
  81.  
  82.   StaticTemp = new TStatic(this, IDC_STATICTEMP, "", 110, 30, 160, 17, 0);
  83.   StaticTemp->Attr.Style |= SS_CENTER;
  84.   Thermometer = new TGauge(this, "%d\xB0", IDC_THERMOMETER, 70, 70, 240, 24, true, 2);
  85.  
  86. TSlider::SetNativeUse(nuNever);
  87.  
  88.   Thermostat = new THSlider(this, IDC_THERMOSTAT, 70, 130, 240, 40);
  89.  
  90.   StaticTime = new TStatic(this, IDC_STATICTIME, "", 4, 10, 160, 17, 0);
  91.   StaticTime->Attr.Style |= SS_LEFT;
  92.   HeaterTime = new TVSlider(this, IDC_HEATERTIME, 20, 30, 32, 160);
  93.  
  94.   StaticOTemp = new TStatic(this, IDC_STATICOTEMP, "", 216, 10, 160, 17, 0);
  95.   StaticOTemp->Attr.Style |= SS_RIGHT;
  96.   OutsideTemp = new TVSlider(this, IDC_OUTSIDETEMP, 330, 30, 32, 160);
  97.  
  98.   BkBrush = new TBrush(TColor::Sys3dFace);
  99.  
  100.   Temp = 70;
  101.   HeaterTimeLeft = 0;
  102. }
  103.  
  104. TTestWindow::~TTestWindow()
  105. {
  106.   delete BkBrush;
  107. }
  108.  
  109. void
  110. TTestWindow::SetupWindow()
  111. {
  112.   TWindow::SetupWindow();
  113.  
  114.   Thermostat->SetRange(40, 120);
  115.   Thermostat->SetRuler(5, false);
  116.   Thermostat->SetPosition(75);
  117.  
  118.   HeaterTime->SetRange(0, 20);
  119.   HeaterTime->SetRuler(2, false);
  120.   HeaterTime->SetPosition(10);
  121.  
  122.   OutsideTemp->SetRange(20, 90);
  123.   OutsideTemp->SetRuler(5, false);
  124.   OutsideTemp->SetPosition(40);
  125.  
  126.   Thermometer->SetRange(40-10, 120+10);
  127.   Thermometer->SetValue(75);
  128.  
  129.   SetTimer(IDT_REFRESH, 1000);
  130.  
  131.   UpdateTemp();
  132.   UpdateHeaterTime();
  133.   UpdateOTemp();
  134. }
  135.  
  136. void
  137. TTestWindow::UpdateTemp()
  138. {
  139.   char str[18];
  140.   sprintf(str, "%s %s", "Heater is ", HeaterTimeLeft ? "On" : "Off");
  141.   StaticTemp->SetText(str);
  142.   Thermometer->SetValue(Temp);
  143. }
  144.  
  145. void
  146. TTestWindow::UpdateHeaterTime(uint)
  147. {
  148.   char str[16];
  149.   sprintf(str, "%d %s", HeaterTime->GetPosition(), "Secs/Cycle");
  150.   StaticTime->SetText(str);
  151. }
  152.  
  153. void
  154. TTestWindow::UpdateOTemp(uint)
  155. {
  156.   char str[14];
  157.   sprintf(str, "%d\xB0 %s", OutsideTemp->GetPosition(), "Outside");
  158.   StaticOTemp->SetText(str);
  159. }
  160.  
  161. //
  162. // Paint a raised, 3d face, background
  163. //
  164. bool
  165. TTestWindow::EvEraseBkgnd(HDC hDC)
  166. {
  167.   TDC dc(hDC);
  168.  
  169.   // SysColors that are bkgnds for text are never dithered & can use FastRect
  170.   dc.TextRect(GetClientRect(), TColor::Sys3dFace);
  171.  
  172.   // These sysColors might be dithered. PaBlt is an easy way to paint these
  173.   TBrush highlight(TColor::Sys3dHilight);
  174.   dc.SelectObject(highlight);
  175.   dc.PatBlt(0, 0, Attr.W, 2);
  176.   dc.PatBlt(0, 2, 2, Attr.H-2);
  177.  
  178.   TBrush shadow(TColor::Sys3dShadow);
  179.   dc.SelectObject(shadow);
  180.   dc.PatBlt(1, Attr.H-2, Attr.W-1, 2);
  181.   dc.PatBlt(Attr.W-2, 1, 2, Attr.H-2-1);
  182.  
  183.   return true;
  184. }
  185.  
  186. //
  187. // Provide a background color & brush for child controls to use
  188. //
  189. HBRUSH
  190. TTestWindow::EvCtlColor(HDC hDC, HWND /*hWndChild*/, uint /*ctlType*/)
  191. {
  192.   ::SetBkColor(hDC, TColor::Sys3dFace);
  193.   return *BkBrush;
  194. }
  195.  
  196. //
  197. // Colors have changed. Rebuild the background brush.
  198. //
  199. void
  200. TTestWindow::EvSysColorChange()
  201. {
  202.   delete BkBrush;
  203.   BkBrush = new TBrush(TColor::Sys3dFace);
  204. }
  205.  
  206. void
  207. TTestWindow::EvTimer(uint /*timerId*/)
  208. {
  209.   Temp += (OutsideTemp->GetPosition()-Temp) / 10;  // heat loss
  210.  
  211.   int tempSetting = Thermostat->GetPosition();   // turn in heater?
  212.   if (!HeaterTimeLeft && Temp < tempSetting-Hysteresis)
  213.     HeaterTimeLeft = HeaterTime->GetPosition();
  214.  
  215.   if (HeaterTimeLeft) {             // heater is running
  216.     HeaterTimeLeft--;
  217.     Temp += 4;                      // heat flows into house
  218.   }
  219.  
  220.   UpdateTemp();
  221. }
  222.  
  223.  
  224. //
  225. // class TTestApp
  226. // ~~~~~ ~~~~~~~~
  227. class TTestApp : public TApplication {
  228.   public:
  229.     TTestApp()
  230.     :
  231.       TApplication()
  232.     {
  233.     }
  234.     void InitMainWindow()
  235.     {
  236.       TFrameWindow* frame = new TFrameWindow(0, "Home Heater Simulator",
  237.         new TTestWindow, true);
  238.       frame->Attr.Style &= ~WS_THICKFRAME;
  239.       frame->EnableKBHandler();
  240.       SetMainWindow(frame);
  241.     }
  242. };
  243.  
  244. int
  245. OwlMain(int /*argc*/, char* /*argv*/ [])
  246. {
  247.   TTestApp app;
  248.   return app.Run();
  249. }
  250.